package com.zhifa.hdtyg_cdgl.quartz;

import com.zhifa.hdtyg_cdgl.dao.SiteReservationMapper;
import com.zhifa.hdtyg_cdgl.entity.SiteReservation;
import com.zhifa.hdtyg_cdgl.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

@Component
public class QuartzObject {

    @Autowired
    private SiteReservationMapper siteReservationMapper;

    @Scheduled(cron = "0 0/1 * * * ?") // 每分钟执行一次
    public void work() throws Exception {
        System.out.println("执行调度任务："+DateUtil.formatDate(new Date(),"yyyy-MM-dd HH:mm:ss"));
    }

    /**
     * 59 45 9 * * ? *
     * 每天的9点45分59秒执行
     * 最近5次运行时间:
     * 2018/6/28 9:45:59
     * 2018/6/29 9:45:59
     * 2018/6/30 9:45:59
     * 2018/7/1 9:45:59
     * 2018/7/2 9:45:59
     *
     * 08:10-09:45
     *
     *
     * 59 50 11 * * ? *
     *每天的11点50分59秒执行
     * 最近5次运行时间:
     * 2018/6/28 11:50:59
     * 2018/6/29 11:50:59
     * 2018/6/30 11:50:59
     * 2018/7/1 11:50:59
     * 2018/7/2 11:50:59
     *
     *
     * 10:15-11:50
     *
     *
     * 59 5 16 * * ? *
     *每天的16点05分59秒执行
     * 最近5次运行时间:
     * 2018/6/27 16:05:59
     * 2018/6/28 16:05:59
     * 2018/6/29 16:05:59
     * 2018/6/30 16:05:59
     * 2018/7/1 16:05:59
     *
     *
     * 14:30-16:05
     *
     *
     * 59 5 18 * * ? *
     * 每天的18点05分59秒执行
     *最近5次运行时间:
     * 2018/6/27 18:05:59
     * 2018/6/28 18:05:59
     * 2018/6/29 18:05:59
     * 2018/6/30 18:05:59
     * 2018/7/1 18:05:59
     *
     *
     * 16:30-18:05
     *
     *
     */

/*

    @Scheduled(fixedRate = 5000)//每5秒执行一次
    public void play() throws Exception {
        System.out.println("执行Quartz定时器任务："+new Date());

        System.out.println( siteReservationMapper.selectByPrimaryKey(1));
    }



    @Scheduled(cron = "0/2 * * * * ?") //每2秒执行一次
    public void doSomething() throws Exception {
        System.out.println("每2秒执行一个的定时任务："+new Date());
    }




    @Scheduled(cron = "0 0 0/1 * * ? ") // 每一小时执行一次
    public void goWork() throws Exception {
        System.out.println("每一小时执行一次的定时任务："+new Date());
    }
*/
    @Scheduled(fixedRate = 5000)//每5秒执行一次
    public void changeWhenTime(){
        System.out.println("=============执行时间段检查============");
        Calendar c = Calendar.getInstance();//可以对每个时间域单独修改

        int year = c.get(Calendar.YEAR);

        int month = c.get(Calendar.MONTH);

        int date = c.get(Calendar.DATE);

        int hour = c.get(Calendar.HOUR_OF_DAY);

        int minute = c.get(Calendar.MINUTE);

        int second = c.get(Calendar.SECOND);

        /**
         *
         * 15:57
         */
       /* hour=18;
        minute=57;*/

        System.out.println(year + "-" + month + "-" + date + " " +hour + ":" +minute + ":" + second);
        /**
         * 08:10-09:45
         * 10:15-11:50
         * 14:30-16:05
         * 16:30-18:05
         */

        if (hour>=9&&minute>45){
          changeTime("08:10-09:45");
        }
        if (hour>=11&&minute>50){
            changeTime("10:15-11:50");
        }
        if (hour>=16&&minute>5){
            changeTime("14:30-16:05");
        }
        if (hour>=18&&minute>5){
            changeTime("16:30-18:05");
        }

    }
    public  void changeTime(String time){
        Date whenTime = new Date();
        SiteReservation siteReservation=new SiteReservation();
        siteReservation.setApplicationTime(whenTime);
        siteReservation.setWhenTime(time);
        siteReservation.setStatus("已过期");
        int i = siteReservationMapper.updateWhenTimeStatus(siteReservation);
        System.out.println("更新了 "+i+" 条数据");
    }


}
